//
//  JUImageGalleryViewController.m
//  JUImageGalleryDemo
//
//  Created by Ju on 15/8/19.
//  Copyright (c) 2015年 juwenz. All rights reserved.
//

#import "JUImageGalleryViewController.h"
#import "JUImageGalleryStepViewController.h"

@interface JUImageGalleryViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate> {
    NSArray *imageURIArray;
    NSString *uriPrefix;
    JUImageGalleryImageFromType type;
    JUImageGalleryStepViewController *currentViewController;
}

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UILabel *pageIndexLabel;



@end

@implementation JUImageGalleryViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = self;
    self.delegate = self;
    
    [self setupPageIndicatorType];
    [self setupStepViewController];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGPoint center = self.view.center;
    switch (self.indicatorType) {
        case JUImageGalleryImageIndicatorPageControl: {
            self.pageControl.numberOfPages = imageURIArray.count;
            center.y = CGRectGetHeight(self.view.frame);
            center.y -= CGRectGetHeight(self.pageControl.frame);
            self.pageControl.center = center;
            break;
        }
        case JUImageGalleryImageIndicatorText: {
            center.y = CGRectGetHeight(self.view.frame);
            center.y -= CGRectGetHeight(self.pageIndexLabel.frame);
            self.pageIndexLabel.center = center;
            
            break;
        }
        default:break;
    }
    
}

- (void)dealloc {
    if ([self.imageLoader respondsToSelector:@selector(cancelAllRequest)]) {
        [self.imageLoader cancelAllRequest];
    }
}

#pragma mark - IBActions


#pragma mark - 公开方法
- (void)configurationWithType:(JUImageGalleryImageFromType)theType imageURIArray:(NSArray *)theImageURIArray URIPrefix:(NSString *)theUriPrefix {
    uriPrefix = theUriPrefix;
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:theImageURIArray.count];
    for (NSString *uri in theImageURIArray) {
        NSString *rebuildURI = [NSString stringWithFormat:@"%@%@",uriPrefix==nil?@"":uriPrefix,uri];
        if (![array containsObject:rebuildURI]) {
            [array addObject:rebuildURI];
        }
    }
    imageURIArray = array;
    type = theType;
    
    [self setupPageIndicatorType];
    [self setupStepViewController];
}

#pragma mark - 协议
#pragma mark UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    currentViewController = (JUImageGalleryStepViewController *)viewController;
    NSInteger offset = [self indexOfViewController:(JUImageGalleryStepViewController *)viewController];
    self.imageOffset = offset;
    return [self stepViewControllerWithOffset:offset+1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger offset = [self indexOfViewController:(JUImageGalleryStepViewController *)viewController];
    self.imageOffset = offset;
    return [self stepViewControllerWithOffset:offset - 1];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return imageURIArray.count;
}

#pragma mark - 私有方法
- (void)setupStepViewController {
    if (imageURIArray.count > 0) {
        JUImageGalleryStepViewController *stepViewController = [self stepViewControllerWithOffset:self.imageOffset];
        self.imageOffset = self.imageOffset;
        [self setViewControllers:@[stepViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
        if (self.indicatorType == JUImageGalleryImageIndicatorText) {
            [self.view bringSubviewToFront:self.pageIndexLabel];
        }else if(self.indicatorType == JUImageGalleryImageIndicatorPageControl){
            [self.view bringSubviewToFront:self.pageControl];
        }
        
    }
}

- (JUImageGalleryStepViewController *)stepViewControllerWithOffset:(NSInteger)offset {
    JUImageGalleryStepViewController *stepViewController = [[JUImageGalleryStepViewController alloc] init];
    stepViewController.type = type;
    if (!self.repeat) {
        if (offset >= imageURIArray.count || offset < 0) {
            return nil;
        }
    }
    stepViewController.imageURI = imageURIArray[offset % imageURIArray.count];
    stepViewController.loaderDelegate = (type==JUImageGalleryImageFromServer)?self.imageLoader:nil;
    return stepViewController;
}

- (NSInteger)indexOfViewController:(JUImageGalleryStepViewController *)viewController {
    return [imageURIArray indexOfObject:viewController.imageURI];
}

- (void)setupPageIndicatorType {
    if (imageURIArray.count <= 0) {
        return;
    }
    switch (self.indicatorType) {
        case JUImageGalleryImageIndicatorPageControl: {
            [self.view addSubview:self.pageControl];
            [self.view bringSubviewToFront:self.pageControl];
            self.pageControl.numberOfPages = imageURIArray.count;
            
            //reset frame according to its content
            CGRect frame = self.pageControl.frame;
            frame.size = [self.pageControl sizeForNumberOfPages:imageURIArray.count];
            frame.size.width += 20;
            self.pageControl.frame = frame;
            
            break;
        }
        case JUImageGalleryImageIndicatorText: {
            [self.view addSubview:self.pageIndexLabel];
            self.pageIndexLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)imageURIArray.count,(long)imageURIArray.count];
            [self.pageIndexLabel sizeToFit];
            CGRect frame = self.pageIndexLabel.frame;
            frame.size.width += 20;
            frame.size.height += 10;
            self.pageIndexLabel.frame = frame;
            [self.view bringSubviewToFront:self.pageIndexLabel];
            break;
        }
        default:break;
    }
}

#pragma mark - 属性
- (void)setImageOffset:(NSInteger)imageOffset {
    _imageOffset = imageOffset;
    switch (self.indicatorType) {
        case JUImageGalleryImageIndicatorPageControl: {
            self.pageControl.currentPage = self.imageOffset;
            break;
        }
        case JUImageGalleryImageIndicatorText: {
            self.pageIndexLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)self.imageOffset+1,(long)imageURIArray.count];
            NSLog(@"%@",self.pageIndexLabel.text);
            break;
        }
        default:break;
    }
    
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.layer.opacity = 0.8;
        _pageControl.layer.cornerRadius = 5;
        _pageControl.clipsToBounds = YES;
        _pageControl.backgroundColor = [UIColor blackColor];
        _pageControl.enabled = NO;
    }
    return _pageControl;
}

- (UILabel *)pageIndexLabel {
    if (!_pageIndexLabel) {
        _pageIndexLabel = [[UILabel alloc] init];
        _pageIndexLabel.backgroundColor = [UIColor blackColor];
        _pageIndexLabel.textColor = [UIColor whiteColor];
        _pageIndexLabel.font = [UIFont boldSystemFontOfSize:16];
        _pageIndexLabel.layer.opacity = 0.8;
        _pageIndexLabel.layer.cornerRadius = 5;
        _pageIndexLabel.clipsToBounds = YES;
        _pageIndexLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _pageIndexLabel;
}


@end
